/**
 * 
 */
package sudoku.ui;

import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import sudoku.controller.Controller;
import sudoku.util.Config;
import sudoku.util.Language;

/**
 * @author Romet
 * 
 */
public class ModifyUserUI implements ActionListener, ChangeListener {

  private volatile SudokuUI m2ng;
  private volatile Controller controller;
  private volatile Config config;
  private volatile Language language;

  private JButton[] sb;
  private JButton[] cb;
  private JButton[] hb;
  private JButton symm;
  private JButton computer;
  private JButton fonts;

  private JLabel[] labels1;
  private JLabel[] labels2;

  private JPanel p1;
  private JPanel p1h;
  private JPanel p2;
  private JPanel panel;

  private JTextField nimi;

  // private JRadioButton sudokuButton;
  // private JRadioButton choiceButton;
  // private JRadioButton mainPanel;
  private JRadioButton esialgne;
  private JRadioButton lahendus;
  private JRadioButton helpButton;

  private JButton seadista;
  private JButton cancel;

  private JDialog dialog;
  private JColorChooser chooser;

  public ModifyUserUI(Language language, SudokuUI m2ng, Controller controller, Config config) {
    this.language = language;
    this.m2ng = m2ng;
    this.controller = controller;
    this.config = config;
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource().equals(symm)) {
      controller.handleEvent(Controller.SYMMETRY_EVENT);
    }
    else if (e.getSource().equals(computer)) {
      controller.handleEvent(Controller.COMPUTERHELP_EVENT);
    }
    else if (e.getSource().equals(fonts)) {
      controller.handleEvent(Controller.FONTSIZE_EVENT);
    }
    else if (e.getSource().equals(cancel)) {
      dialog.dispose();
    }
    else {
      String s = nimi.getText() + ":";
      s += sb[0].getForeground().getRed() + "," + sb[0].getForeground().getGreen() + "," + sb[0].getForeground().getBlue() + ";";
      s += sb[3].getForeground().getRed() + "," + sb[3].getForeground().getGreen() + "," + sb[3].getForeground().getBlue() + ";";
      s += labels1[0].getForeground().getRed() + "," + labels1[0].getForeground().getGreen() + "," + labels1[0].getForeground().getBlue();
      if (config.getHetke_kasutaja() == -1) {
        List<String> list = config.getKasutajad();
        list.add(s);
        config.setHetke_kasutaja(config.getKasutajad().size() - 1);
        config.setKasutajad(list);
      }
      else {
        List<String> list = config.getKasutajad();
        list.remove(config.getHetke_kasutaja());
        list.add(config.getHetke_kasutaja(), s);
        config.setHetke_kasutaja(config.getHetke_kasutaja());
        config.setKasutajad(list);
      }
      dialog.dispose();
      controller.handleEvent(Controller.VALITUD_KASUTAJA_EVENT);
    }
  }

  public void drawWindow() {
    labels1 = new JLabel[2];
    for (int i = 0; i < labels1.length; i++) {
      labels1[i] = new JLabel("" + (i + 1));
      labels1[i].setForeground(config.getHelpButton());
      labels1[i].setFont(new Font(null, Font.PLAIN, 10));
    }
    labels2 = new JLabel[2];
    for (int i = 0; i < labels2.length; i++) {
      labels2[i] = new JLabel("" + (i + 1));
      labels2[i].setForeground(config.getHelpButton());
      labels2[i].setFont(new Font(null, Font.PLAIN, 10));
    }

    panel = new JPanel();
    panel.setLayout(new GridBagLayout());
    panel.setBorder(BorderFactory.createTitledBorder(language.getModifyUsers_expl()));
    GridBagConstraints constraints = new GridBagConstraints();

    p1 = new JPanel();
    new BoxLayout(p1, BoxLayout.Y_AXIS);
    sb = new JButton[5];
    for (int i = 0; i < sb.length; i++) {
      sb[i] = new JButton(String.valueOf(i + 1));
      sb[i].setFocusPainted(false);
      if (i < 2) {
        sb[i].setForeground(config.getEsialgne());
      }
      else {
        sb[i].setForeground(config.getLahendus());
      }
      sb[i].setBackground(config.getSudokuButton());
      p1.add(sb[i]);
    }
    p1.setBorder(BorderFactory.createTitledBorder(language.getModifyUsers_sbutt()));
    constraints.gridx = 0;
    constraints.gridy = 0;
    panel.add(p1, constraints);

    p1h = new JPanel();
    new BoxLayout(p1h, BoxLayout.Y_AXIS);
    hb = new JButton[2];
    for (int i = 0; i < hb.length; i++) {
      hb[i] = new JButton();
      hb[i].setFocusPainted(false);
      hb[i].setLayout(new GridLayout(1, 2));
      if (i == 1) {
        for (int j = 0; j < labels1.length; j++) {
          hb[i].add(labels1[j]);
        }
      }
      else {
        for (int j = 0; j < labels2.length; j++) {
          hb[i].add(labels2[j]);
        }
      }
      hb[i].setBackground(config.getSudokuButton());
      p1h.add(hb[i]);
    }
    p1h.setBorder(BorderFactory.createTitledBorder(language.getModifyUsers_hbutt()));
    constraints.gridx = 1;
    constraints.gridy = 0;
    panel.add(p1h, constraints);

    p2 = new JPanel();
    new BoxLayout(p2, BoxLayout.Y_AXIS);
    cb = new JButton[5];
    for (int i = 0; i < cb.length; i++) {
      cb[i] = new JButton(String.valueOf(i + 1));
      cb[i].setFocusPainted(false);
      if (i < 2) {
        cb[i].setForeground(config.getEsialgne());
      }
      else {
        cb[i].setForeground(config.getLahendus());
      }
      cb[i].setBackground(config.getChoiceButton());
      p2.add(cb[i]);
    }

    p2.setBorder(BorderFactory.createTitledBorder(language.getModifyUsers_cbutt()));
    constraints.gridx = 0;
    constraints.gridy = 1;
    constraints.gridwidth = 2;
    panel.add(p2, constraints);

    nimi = new JTextField(language.getConfig_nimi());
    ButtonGroup group = new ButtonGroup();
    esialgne = new JRadioButton(language.getModifyUsers_etext());
    esialgne.setSelected(true);
    lahendus = new JRadioButton(language.getModifyUsers_ltext());
    helpButton = new JRadioButton(language.getModifyUsers_htext());
    group.add(helpButton);
    group.add(esialgne);
    group.add(lahendus);

    seadista = new JButton(language.getModifyUsers_ok());
    seadista.addActionListener(this);

    cancel = new JButton(language.getButton_cancel());
    cancel.addActionListener(this);

    chooser = new JColorChooser();
    chooser.getSelectionModel().addChangeListener(this);
    chooser.remove(1);
    chooser.removeChooserPanel(chooser.getChooserPanels()[1]);
    chooser.removeChooserPanel(chooser.getChooserPanels()[1]);

    symm = new JButton(language.getSudoku_symm());
    symm.addActionListener(this);
    computer = new JButton(language.getComputerhelp());
    computer.addActionListener(this);
    fonts = new JButton(language.getSudoku_fonts_menu());
    fonts.addActionListener(this);

    dialog = new JDialog(m2ng, language.getModifyUsers_title(), true);
    dialog.setLayout(new GridBagLayout());

    GridBagConstraints c = new GridBagConstraints();

    c.gridx = 0;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    dialog.add(symm, c);

    c.gridx = 1;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    dialog.add(computer, c);

    c.gridx = 2;
    c.gridy = 0;
    c.fill = GridBagConstraints.BOTH;
    c.anchor = GridBagConstraints.CENTER;
    dialog.add(fonts, c);

    c.gridx = 0;
    c.gridy = 1;
    c.fill = GridBagConstraints.BOTH;
    c.gridwidth = 3;
    dialog.add(panel, c);

    c.gridx = 0;
    c.gridy = 2;
    c.fill = GridBagConstraints.BOTH;
    c.gridwidth = 1;
    JLabel label = new JLabel(language.getModifyUsers_name());
    label.setHorizontalAlignment(JLabel.RIGHT);
    dialog.add(label, c);

    c.gridx = 1;
    c.gridy = 2;
    c.fill = GridBagConstraints.BOTH;
    c.gridwidth = 2;
    dialog.add(nimi, c);

    c.gridx = 0;
    c.gridy = 3;
    c.gridwidth = 1;
    dialog.add(esialgne, c);

    c.gridx = 1;
    c.gridy = 3;
    c.gridwidth = 1;
    dialog.add(lahendus, c);

    c.gridx = 2;
    c.gridy = 3;
    c.gridwidth = 1;
    dialog.add(helpButton, c);

    c.gridx = 0;
    c.gridy = 4;
    c.gridwidth = 3;
    dialog.add(chooser, c);

    c.gridx = 1;
    c.gridy = 5;
    c.gridwidth = 1;
    dialog.add(seadista, c);

    c.gridx = 2;
    c.gridy = 5;
    c.gridwidth = 1;
    dialog.add(cancel, c);

    dialog.pack();
    Point point = m2ng.getLocation();
    dialog.setLocation(point.x + m2ng.getWidth() / 2 - dialog.getWidth() / 2, point.y + m2ng.getHeight() / 2 - dialog.getHeight() / 2);
    dialog.setVisible(true);
  }

  @Override
  public void stateChanged(ChangeEvent e) {
    // if (sudokuButton.isSelected()){
    // for (int i = 0; i < sb.length; i++) {
    // sb[i].setBackground(chooser.getColor());
    // }
    // for (int i = 0; i < hb.length; i++) {
    // hb[i].setBackground(chooser.getColor());
    // }
    // }
    // else if (choiceButton.isSelected()) {
    // for (int i = 0; i < cb.length; i++) {
    // cb[i].setBackground(chooser.getColor());
    // }
    // }
    // else if (mainPanel.isSelected()) {
    // panel.setBackground(chooser.getColor());
    // p1.setBackground(chooser.getColor());
    // p2.setBackground(chooser.getColor());
    // }
    if (esialgne.isSelected()) {
      for (int i = 0; i < 2; i++) {
        sb[i].setForeground(chooser.getColor());
        cb[i].setForeground(chooser.getColor());
      }
    }
    else if (lahendus.isSelected()) {
      for (int i = 2; i < 5; i++) {
        sb[i].setForeground(chooser.getColor());
        cb[i].setForeground(chooser.getColor());
      }
    }
    else if (helpButton.isSelected()) {
      for (int j = 0; j < labels1.length; j++) {
        labels1[j].setForeground(chooser.getColor());
      }
      for (int j = 0; j < labels1.length; j++) {
        labels2[j].setForeground(chooser.getColor());
      }
    }
  }

}
